define(['text!/template/${moduleMapperCode}/${(businessCode?uncap_first)!''}/index.tpl.html'], function (indexTpl) {
    'use strict';
    return Marionette.LayoutView.extend({
        id: _.uniqueId("${moduleMapperCode}-${(businessCode?uncap_first)!''}-index-"),
        className: "pure-g",
        events: {
            'click [data-id="form-1"] a[data-page-number]': "onSearch",
            'click [data-id="table-1"] a[data-page-number]': "onSearch",
            'change [data-id="table-1"] select[data-page-number]': "onSearch",
            'click [data-id="form-1"] a[data-id="reset"]': "onReset",
            'click a[data-id="add"]': "onAdd",
            'click a[data-id="modify"]': "onModify",
            'click a[data-id="delete"]': "onDelete"
        },
        serializeData: function () {
            var data = this.options.data;

            return data;
        },
        initialize: function () {
            this.template = _.template(indexTpl);
        },
        onRender: function () {
            C1.joinCheckboxs({
                $el: this.$el,
                mainSelector: '[data-id="table-1"] input[name="all-${(businessCode?uncap_first)!''}Id"]',
                childSelector: '[data-id="table-1"] input[name="${(businessCode?uncap_first)!''}Id"]'
            });

            _.delay(function (that) {
                that.$el.tooltip(C1.tooltipDefaultOptions);

                var $startTime = that.$('input[name="startTime"]');

                var $endTime = that.$('input[name="endTime"]');

                $startTime.datepicker({
                    dateFormat: "yy-mm-dd"
                }).on("change", function () {
                    $endTime.datepicker("option", "minDate", that.getDate(this));
                });

                $endTime.datepicker({
                    dateFormat: "yy-mm-dd"
                }).on("change", function () {
                    $startTime.datepicker("option", "maxDate", that.getDate(this));
                });
            }, 500, this);
        },
        getDate: function (element) {
            try {
                return $.datepicker.parseDate("yy-mm-dd", element.value);
            } catch (error) {
                return null;
            }
        },
        onSearch: function (event) {
            var pageNumber = 1;

            if (event) {
                event.preventDefault();

                pageNumber = this.$(event.currentTarget).data("page-number") || this.$('[data-id="form-1"] input[name="pageNumber"]').val();
            }

            this.$('[data-id="form-1"] input[name="pageNumber"]').val(pageNumber);

            var pageSize = this.$('[data-id="table-1"] select[name="pageSize"]').val() || this.$('[data-id="form-1"] input[name="pageSize"]').val();

            this.$('[data-id="form-1"] input[name="pageSize"]').val(pageSize);

            var data = C1.serializeToJson(this.$('[data-id="form-1"]'));

            var $table1 = this.$('[data-id="table-1"]');

            var tableTemplateUrl = $table1.data("table-template-url");

            var tableApiUrl = $table1.data("table-api-url");

            $.ajax({
                url: tableApiUrl,
                data: data,
                onSuccess: function (data, textStatus, jqXHR) {
                    C1.loadTemplate.call(this, tableTemplateUrl, function (tableTemplate) {
                        data.pageSize = pageSize;

                        $table1.html(tableTemplate(data));
                    });
                }
            });
        },
        onReset: function (event) {
            if (event) {
                event.preventDefault();
            }

            this.$('[data-id="form-1"] input[type="reset"]').click();
        },
        onAdd: function (event) {
            if (event) {
                event.preventDefault();
            }

            var that = this;

            var $table1 = this.$('[data-id="table-1"]');

            var addModalUrl = $table1.data("add-modal-url");

            C1.loadView({
                context: this,
                url: addModalUrl,
                successCallback: function (addModalView) {
                    addModalView.options.saveSuccess = function () {
                        that.onSearch();
                    };

                    C1.Application.vent.trigger("appendShow", addModalView);
                }
            });
        },
        onModify: function (event) {
            if (event) {
                event.preventDefault();
            }

            var ${(businessCode?uncap_first)!''}Id = this.$el.selectorVal('[data-id="table-1"] input[name="${(businessCode?uncap_first)!''}Id"]:checked');

            if (!${(businessCode?uncap_first)!''}Id) {
                return;
            }

            var data = {
                ${(businessCode?uncap_first)!''}Id: ${(businessCode?uncap_first)!''}Id
            };

            var that = this;

            var $table1 = this.$('[data-id="table-1"]');

            var modifyModalUrl = $table1.data("modify-modal-url");

            C1.loadView({
                context: this,
                data: data,
                url: modifyModalUrl,
                successCallback: function (modifyModal) {
                    modifyModal.options.saveSuccess = function () {
                        that.onSearch();
                    };

                    C1.Application.vent.trigger("appendShow", modifyModal);
                }
            });
        },
        onDelete: function (event) {
            if (event) {
                event.preventDefault();
            }
			
            var ${(businessCode?uncap_first)!''}Ids = this.$el.selectorsValJoinToString('[data-id="table-1"] input[name="${(businessCode?uncap_first)!''}Id"]:checked');

            if (!${(businessCode?uncap_first)!''}Ids) {
                return;
            }

            var data = {
                ${(businessCode?uncap_first)!''}Ids: ${(businessCode?uncap_first)!''}Ids
            };

            var that = this;

            C1.modal.confirm("确认删除！", function () {
                var $table1 = that.$('[data-id="table-1"]');

                $.ajax({
                    url: $table1.data("delete-api-url"),
                    data: data,
                    onSuccess: function (data, textStatus, jqXHR) {
                        C1.modal.alert("删除成功");

                        that.onSearch();
                    }
                });
            });
        }
    });
});